<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/7
 * Time: 14:26
 */

namespace app\common\model;

use think\Db;
use think\Model;

class GiftRecord extends Model
{

    public function users()
    {
        return $this->hasOne('users','user_id','to_user');
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 房间礼物统计
     */
    public function roomCount($map = [])
    {
        $field = 'sum(total) as total,u.nick_name,u.header_img,a.to_user';
        $rows =  $this->alias('a')
            ->join([
                ['users u','u.user_id = a.to_user','left'],
            ])
            ->where($map)
            ->field($field)
            ->group('to_user')
            ->order('total desc')
            ->paginate()->each(function ($item){
                $item['to_user'] = hashid($item['to_user']);
            });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }

    public function giftCount($where = [])
    {
        return $this->alias('a')
            ->join([
                ['gift g','g.gift_id = a.gift_id','left']
            ])
            ->where($where)
            ->field('g.gift_id,g.gift_name,g.img,sum(a.num) as num,g.thumbnail,g.price')
            ->group('gift_id')
            ->cache(2)
            ->order('g.price')
            ->select();
    }




    public function giftList($map = [])
    {
        $rows =  $this->alias('a')
            ->join([
                ['users u','u.user_id = a.user_id','left'],
                ['gift g','g.gift_id = a.gift_id','left']
            ])
            ->where($map)
            ->field('a.total,u.nick_name,u.header_img,a.to_user,a.create_time,g.gift_name,g.thumbnail,a.num,a.user_id')
            ->cache(15)
            ->order('a.record_id desc')
            ->paginate()->each(function ($item){
                $item['format_time'] = formatTime2($item['create_time']);
                $item['user_id'] = hashid($item['user_id']);
                $item['to_user_id'] = hashid($item['to_user']);
                $item['to_user_header_img'] = \app\api\controller\Base::staticInfo('header_img',$item['to_user']);
                $item['to_user'] = \app\api\controller\Base::staticInfo('nick_name',$item['to_user']);
            });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }


    public function _list($map = [])
    {
        $rows =  $this->alias('a')
            ->join([
                ['users u','u.user_id = a.user_id','left'],
                ['gift g','a.gift_id = g.gift_id','left'],
            ])
            ->where($map)
            ->field('a.total,u.nick_name,u.header_img,a.to_user,a.create_time,g.thumbnail,g.gift_name,a.num')
            ->cache(15)
            ->order('a.record_id desc')
            ->paginate()->each(function ($item){
                $item['to_user'] = hashid($item['to_user']);
            });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }


    public function getTrueLove($map = [])
    {

        $field = 'a.user_id,u.nick_name,u.header_img,e.noble_id,e.level,e.noble_time,a.to_user,g.thumbnail,g.img,a.create_time,r.room_id,r.room_name';

        $rows =  $this->alias('a')
            ->join([
                ['users u','u.user_id = a.user_id','left'],
                ['user_extend e','e.user_id = a.user_id','left'],
                ['gift g','g.gift_id = a.gift_id','left'],
                ['room r','r.room_id = a.room_id','left'],
            ])
            ->where($map)
            ->field($field)
            ->cache(15)
            ->order('a.record_id desc')
            ->paginate(3)->each(function ($item){
                $item['formatTime'] = formatTime($item['create_time'],false);
                $item['noble_id'] = \app\api\controller\Base::checkNoble($item);
                $item['to_nick_name'] = \app\api\controller\Base::staticInfo('nick_name',$item['to_user']);
                $item['to_header_img'] = \app\api\controller\Base::staticInfo('header_img',$item['to_user']);
                $item['to_user'] = hashid($item['to_user']);
                $item['user_id'] = hashid($item['user_id']);
            });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }

    public function protect($map = [])
    {



        $field = 'sum(total) as total,a.user_id,u.nick_name,u.header_img,e.noble_id,e.level,e.noble_time,a.to_user,a.create_time,r.room_id,r.room_name';

        $rows =  $this->alias('a')
            ->join([
                ['users u','u.user_id = a.user_id','left'],
                ['user_extend e','e.user_id = a.user_id','left'],
                ['room r','r.room_id = a.room_id','left'],
            ])
            ->where($map)
            ->field($field)
            ->group('to_user')
            ->cache(15)
            ->order('total desc')
            ->paginate(3)->each(function ($item){
                $item['formatTime'] = formatTime($item['create_time'],false);
                $item['noble_id'] = \app\api\controller\Base::checkNoble($item);
                $item['to_nick_name'] = \app\api\controller\Base::staticInfo('nick_name',$item['to_user']);
                $item['to_header_img'] = \app\api\controller\Base::staticInfo('header_img',$item['to_user']);
                $item['to_user'] = hashid($item['to_user']);
                $item['user_id'] = hashid($item['user_id']);
            });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }


    //后台获取赠送礼物日志
    public function  recordList($where=[])
    {
        return $this->alias('gr')->where($where)
            ->join('gift g','gr.gift_id = g.gift_id','left')
            ->join('users u','gr.user_id = u.user_id','left')
            ->join('users us','gr.to_user = us.user_id','left')
            ->field('g.gift_name,g.thumbnail,g.price,gr.num,gr.create_time,gr.update_time,u.nick_name,gr.to_user,u.uuid,us.uuid as us_uuid')
            ->order('gr.record_id desc')
            ->paginate('',false,['query'=>request()->param()]);
    }

    public function myDetail($where = [],$type)
    {
        if ($type == 1){
            $rows = $this->alias('a')
                ->where($where)
                ->field('a.user_id,a.num,a.gift_id,a.create_time')
                ->cache(60)
                ->order('record_id desc')
                ->paginate();
        }else{
            $rows = $this->alias('a')
                ->where($where)
                ->field('a.to_user as user_id,a.num,a.gift_id,a.create_time')
                ->cache(60)
                ->order('record_id desc')
                ->paginate();
        }
        $items = $rows->items();
        foreach ($items as $k => $v){
            $items[$k]['nick_name'] = \app\api\controller\Base::staticInfo('nick_name',$v['user_id']);
            $items[$k]['header_img'] = \app\api\controller\Base::staticInfo('header_img',$v['user_id']);
            $items[$k]['gift_name'] = Db::name('gift')->where('gift_id',$v['gift_id'])->cache(60)->value('gift_name');
            $items[$k]['img'] = Db::name('gift')->where('gift_id',$v['gift_id'])->cache(60)->value('img');
            $items[$k]['thumbnail'] = Db::name('gift')->where('gift_id',$v['gift_id'])->cache(60)->value('thumbnail');
            $items[$k]['user_id'] = hashid($v['user_id']);
        }
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$items];
    }

    /**
     * @param array $map
     * @param string $user
     * @return array
     * @throws \think\exception\DbException
     */
    public function ranking($map = [], $user = 'to_user')
    {
        $field = 'sum(total) as total,a.user_id,a.to_user,a.create_time';

        $rows  =  $this->alias('a')
                    ->where($map)
                    ->field($field)
                    ->group($user)
                    ->cache(15)
                    ->order('total desc')
                    ->paginate()->each(function ($item)use ($user){
                        $item['nick_name'] = \app\api\controller\Base::staticInfo('nick_name',$item[$user]);
                        $item['header_img'] = \app\api\controller\Base::staticInfo('header_img',$item[$user]);
                        $item['sign'] = \app\api\controller\Base::staticInfo('sign',$item[$user]);
                        $item['user_id'] = hashid($item[$user]);
                        unset($item['to_user']);
                    });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }

    /**
     * @param array $map
     * @param string $user
     * @return array
     * @throws \think\exception\DbException
     */
    public function starList($map = [], $user = 'to_user')
    {
        $field = 'sum(total) as total,a.user_id,a.to_user,a.create_time';

        $rows  =  self::alias('a')
            ->where($map)
            ->field($field)
            ->group($user)
            ->cache(15)
            ->order('total desc')
            ->paginate(30)->each(function ($item)use ($user){
                $item['nick_name'] = \app\api\controller\Base::staticInfo('nick_name',$item[$user]);
                $item['header_img'] = \app\api\controller\Base::staticInfo('header_img',$item[$user]);
                $item['sign'] = \app\api\controller\Base::staticInfo('sign',$item[$user]);
                $item['headWear'] = \app\api\controller\Base::getHeadWear($item['user_id']);
                $item['user_id'] = hashid($item[$user]);
                unset($item['to_user']);
            });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }

    /**
     * @param array $map
     * @param string $user
     * @return array
     * @throws \think\exception\DbException
     */
    public function richList($map = [], $user = 'to_user')
    {
        $field = 'sum(total) as total,a.user_id,a.to_user,a.create_time';

        $rows  =  self::alias('a')
            ->where($map)
            ->field($field)
            ->group($user)
            ->cache(15)
            ->order('total desc')
            ->paginate(30)->each(function ($item)use ($user){
                $item['nick_name'] = \app\api\controller\Base::staticInfo('nick_name',$item[$user]);
                $item['header_img'] = \app\api\controller\Base::staticInfo('header_img',$item[$user]);
                $item['sign'] = \app\api\controller\Base::staticInfo('sign',$item[$user]);
                $item['headWear'] = \app\api\controller\Base::getHeadWear($item['user_id']);
                $item['user_id'] = hashid($item[$user]);
                unset($item['to_user']);
            });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }
    
    /**
     * @param array $map
     * @param string $user
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function weekList($map = [], $user = 'to_user')
    {
        $field = 'sum(total) as total,a.user_id,a.to_user,a.create_time';

        $rows  =  $this->alias('a')
            ->where($map)
            ->field($field)
            ->group($user)
            ->cache(15)
            ->order('total desc')
            ->paginate()->each(function ($item)use ($user){
                $item['nick_name'] = \app\api\controller\Base::staticInfo('nick_name',$item[$user]);
                $item['header_img'] = \app\api\controller\Base::staticInfo('header_img',$item[$user]);
                $item['sign'] = \app\api\controller\Base::staticInfo('sign',$item[$user]);
                $item['user_id'] = hashid($item[$user]);
                unset($item['to_user']);
            });
        $gift = Gift::where('is_naming',1)->field('gift_id,gift_name,thumbnail')->select();
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items(),'gift'=> $gift];
    }

    public static function weekUser(array $map)
    {
        return self::alias('a')
            ->join('users u','a.user_id = u.user_id','left')
            ->where($map)
            ->field('sum(total) as total,a.user_id,u.nick_name')
            ->group('user_id')
            ->order('total desc')
            ->find();
    }

}